﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YunCMS.BLL;
using YunCMS.Model;
using YunCMS.Common;
using System.Web.UI.WebControls;

namespace YunCMS.Controls.Page
{
    public abstract class ModuleListPage<T> : AuthPage
        where T : YCModule, new()
    {

        protected abstract ModuleBLL<T> BLL
        {
            get;
        }

        protected void FirstLoad()
        {
            string action = Request.QueryString["action"];
            int id;
            if (!string.IsNullOrEmpty(action))
            {
                string _id = Request.QueryString["id"];

                if (!int.TryParse(_id, out id))
                    id = 0;
                if (id != 0)
                    HandleAction(action, id);
            }

            BindControls(true);
        }

        protected override string Module
        {
            get
            {
                return "ContentManage";
            }
        }

        protected virtual IList<int> AllSelectedItems(Repeater dataListRP)
        {
            List<int> ids = new List<int>() { };
            for (int i = 0; i < dataListRP.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)dataListRP.Items[i].FindControl("idCHB");
                if (cb.Checked)
                {
                    int id = Convert.ToInt32(((Label)dataListRP.Items[i].FindControl("idLabel")).Text);
                    ids.Add(id);
                }
            }
            return ids;
        }

        protected abstract void BindControls(bool isFirst);
       
        protected virtual void HandleAction(string action, int id)
        {
            YCModuleArticalBLL bll = new YCModuleArticalBLL();
            string qval = Request.QueryString["val"];
            switch (action)
            {
                case "delete":
                    bll.Remove(id);
                    break;
                case "top":
                    if (!string.IsNullOrEmpty(qval))
                    {
                        bool val = Convert.ToBoolean(qval);
                        bll.UpdateIsHome(id, !val);
                    }
                    break;
                case "pop":
                    if (!string.IsNullOrEmpty(qval))
                    {
                        bool val = Convert.ToBoolean(qval);
                        bll.UpdateIsPop(id, !val);
                    }
                    break;
                case "active":
                    if (!string.IsNullOrEmpty(qval))
                    {
                        bool val = Convert.ToBoolean(qval);
                        bll.UpdateActive(id, !val);
                    }
                    break;
                default: break;
            }
        }
    }
}
